<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
class News extends Base{
    //广告图
    public function news_banner(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('news_banner')->where('id',1)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['banner']=$param['banner'];
            $data['title_en']=$param['title_en'];
            $res= Db::name('news_banner')->where(array('id'=>1))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function news_list(){
        $keyword=input('get.keyword');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $list=Db::name('news')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('title','like',"%{$keyword}%")->where('state','<',3)->order('id desc')->paginate(10);
        $counts=Db::name('news')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('title','like',"%{$keyword}%")->where('state','<',3)->count();
        $data=$list->all();
        $this->assign('keyword',input('get.keyword'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function news_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['author']=$param['author'];
            $data['views']=$param['views'];
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            $data['seo_keywords']=$param['seo_keywords'];
            $data['seo_description']=$param['seo_description'];
            $data['sort']=$param['sort'];
            if($param['top']==1){
                $data['top']=$param['top'];
                Db::name('news')->where('state',1)->update(['top'=>2]);
            }
            $data['time']=time();
            $data['uptime']=time();
            $res= Db::name('news')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function news_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('news')->where('id',$param['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['author']=$param['author'];
            $data['views']=$param['views'];
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            $data['seo_keywords']=$param['seo_keywords'];
            $data['seo_description']=$param['seo_description'];
            $data['sort']=$param['sort'];
            if($param['top']==1){
                $data['top']=$param['top'];
                Db::name('news')->where('state',1)->update(['top'=>2]);
            }
            $data['uptime']=time();
            $res=Db::name('news')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function news_del(){
        $param = input('post.');
        $res= Db::name('news')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }

    public function innews_list(){
        $keyword=input('get.keyword');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $list=Db::name('industry_news')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('title','like',"%{$keyword}%")->where('state','<',3)->order('id desc')->paginate(10);
        $counts=Db::name('industry_news')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('title','like',"%{$keyword}%")->where('state','<',3)->count();
        $data=$list->all();
        $this->assign('keyword',input('get.keyword'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function innews_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['author']=$param['author'];
            $data['views']=$param['views'];
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            $data['seo_keywords']=$param['seo_keywords'];
            $data['seo_description']=$param['seo_description'];
            $data['sort']=$param['sort'];
            if($param['top']==1){
                $data['top']=$param['top'];
                Db::name('industry_news')->where('state',1)->update(['top'=>2]);
            }
            $data['time']=time();
            $data['uptime']=time();
            $res= Db::name('industry_news')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function innews_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('industry_news')->where('id',$param['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['author']=$param['author'];
            $data['views']=$param['views'];
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            $data['seo_keywords']=$param['seo_keywords'];
            $data['seo_description']=$param['seo_description'];
            $data['sort']=$param['sort'];
            if($param['top']==1){
                $data['top']=$param['top'];
                Db::name('industry_news')->where('state',1)->update(['top'=>2]);
            }
            $data['uptime']=time();
            $res=Db::name('industry_news')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function innews_del(){
        $param = input('post.');
        $res= Db::name('industry_news')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function expnews_list(){
        $keyword=input('get.keyword');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $list=Db::name('express_news')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('title','like',"%{$keyword}%")->where('state','<',3)->order('id desc')->paginate(10);
        $counts=Db::name('express_news')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('title','like',"%{$keyword}%")->where('state','<',3)->count();
        $data=$list->all();
        $this->assign('keyword',input('get.keyword'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function expnews_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['author']=$param['author'];
            $data['views']=$param['views'];
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            $data['seo_keywords']=$param['seo_keywords'];
            $data['seo_description']=$param['seo_description'];
            $data['sort']=$param['sort'];
            $data['time']=time();
            $data['uptime']=time();
            $res= Db::name('express_news')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function expnews_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('express_news')->where('id',$param['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['author']=$param['author'];
            $data['views']=$param['views'];
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            $data['seo_keywords']=$param['seo_keywords'];
            $data['seo_description']=$param['seo_description'];
            $data['sort']=$param['sort'];
            $data['uptime']=time();
            $res=Db::name('express_news')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function expnews_del(){
        $param = input('post.');
        $res= Db::name('express_news')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
}